package cn.wolfcode.car.business.web.controller.system;


import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController {
    //模板前缀
    private static final String prefix = "business/carPackageAudit/";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IStatementService statementService;


    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo){

        return carPackageAuditService.query(qo);
    }
@RequestMapping("/processImg")
    public void processImg(Long id , HttpServletResponse response) throws IOException {
        InputStream inputStream = carPackageAuditService.getProcessImgById(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

@RequestMapping("/cancelApply")
@ResponseBody
    public AjaxResult cancelApply(Long id){
        carPackageAuditService.cancelApply(id);
    return AjaxResult.success();
    }
    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "/todoPage";
    }

    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        Long userId = ShiroUtils.getUserId();
        qo.setId(userId);
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.query(qo);
    }

    /**
     * 跳到审批页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("auditPage")
    public String auditPage(Model model,Long id){
        model.addAttribute("id",id);
        return prefix + "/audit";
    }


    /**
     *  确认审批操作
     * @param id
     * @param auditStatus
     * @param info
     * @return
     */
    @RequestMapping("audit")
    @ResponseBody
    public AjaxResult audit(Long id,Integer auditStatus,String info){
        carPackageAuditService.audit(id,auditStatus,info);

        return AjaxResult.success();
    }
    @RequestMapping("donePage")
    public String donePage(){

        return prefix + "/donePage";
    }

    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery qo){
        Long userId = ShiroUtils.getUserId();
        qo.setId(userId);
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.query(qo);
    }
}
